package com.example.server.handler;

import com.example.message.LoginRequestMessage;
import com.example.message.LoginResponseMessage;
import com.example.server.service.UserServiceFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class LoginRequestMessageHandler extends SimpleChannelInboundHandler<LoginRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginRequestMessage message) throws Exception {
        String username = message.getUsername();
        String password = message.getPassword();
        // todo 校验数据
        LoginResponseMessage responseMessage = null;
        boolean login = UserServiceFactory.getUserService().login(username, password);
        if (login) {
            responseMessage = new LoginResponseMessage(true, "登录ok.");
        } else {
            responseMessage = new LoginResponseMessage(false, "登录失败.");
        }
        // todo  返回给客户端
        ctx.writeAndFlush(responseMessage);
    }
}
